package cn.com.dcsgo.controller;

import cn.com.dcsgo.domain.SysUser;
import cn.com.dcsgo.dto.PageDTO;
import cn.com.dcsgo.dto.SysUserAddDTO;
import cn.com.dcsgo.dto.SysUserQueryDTO;
import cn.com.dcsgo.dto.SysUserUpdateDTO;
import cn.com.dcsgo.model.Result;
import cn.com.dcsgo.service.SysUserService;
import cn.com.dcsgo.utils.AuthUtil;
import cn.com.dcsgo.vo.SysUserVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台系统用户管理相关接口
 *
 * @author Dcsgo
 * @since 2025/9/22 17:19
 */
@RestController
@RequestMapping("sys/user")
@Tag(name = "后台系统用户管理相关接口")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;

    /**
     * 获取登录用户信息
     *
     * @return 登录用户信息(不包含密码)
     */
    @GetMapping("info")
    @Operation(summary = "获取登录用户信息(不包含密码)")
    public Result<SysUser> getUserInfo() {
        SysUser user = sysUserService.getById(AuthUtil.getUserId());
        user.setPassword(null);
        return Result.success(user);
    }

    /**
     * 分页查询用户信息
     *
     * @param pageDTO         分页请求参数
     * @param sysUserQueryDTO 查询后台管理用户的请求参数
     * @return 用户信息分页数据(按创建时间升序排序)
     */
    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys:user:page')")
    @Operation(summary = "分页查询用户信息")
    public Result<Page<SysUser>> getUserPage(PageDTO pageDTO, SysUserQueryDTO sysUserQueryDTO) {
        Page<SysUser> page = sysUserService.getPage(pageDTO, sysUserQueryDTO);
        return Result.success(page);
    }

    /**
     * 添加用户
     *
     * @return HTTP统一返回对象
     */
    @PostMapping
    @PreAuthorize("hasAuthority('sys:user:save')")
    @Operation(summary = "添加用户")
    public Result<Void> addUser(@RequestBody @Validated SysUserAddDTO sysUserAddDTO) {
        int row = sysUserService.addUser(sysUserAddDTO);
        return Result.handle(row > 0);
    }

    @GetMapping("info/{id}")
    @PreAuthorize("hasAuthority('sys:user:info')")
    @Operation(summary = "根据ID获取用户信息", description = "用户信息（不包含密码和店铺ID，包含角色ID列表）")
    public Result<SysUserVO> getUserInfo(@PathVariable("id") Long id) {
        SysUserVO user = sysUserService.getUserInfoById(id);
        return Result.success(user);
    }

    @PutMapping
    @PreAuthorize("hasAuthority('sys:user:update')")
    @Operation(summary = "修改用户信息", description = "可以一并改密码和角色")
    public Result<Void> updateUser(@RequestBody @Validated SysUserUpdateDTO sysUserUpdateDTO) {
        int row = sysUserService.updateUser(sysUserUpdateDTO);
        return Result.handle(row > 0);
    }

    @DeleteMapping("{ids}")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    @Operation(summary = "删除用户", description = "一并删除用户角色关系表中的数据，根据id个数决定是否批量删除，2个及以上批量删除")
    public Result<Void> deleteUsers(@PathVariable("ids")
                                    @Parameter(description = "用户ID列表", example = "2,3,4或1")
                                    List<Long> ids) {
        int row;
        if (ids.size() == 1) {
            row = sysUserService.removeUserById(ids.get(0));
        } else {
            row = sysUserService.removeUserByIds(ids);
        }
        return Result.handle(row > 0);
    }
}
